package com.jneagle.xlstool.bzwgjmx.view.gui;

import com.jneagle.xlstool.bzwgjmx.view.handler.I18nHandler;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

/**
 * 国际化表头渲染器。
 *
 * @author DwArFeng
 * @since 1.0.0
 */
@org.springframework.stereotype.Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class I18nTableCellRenderer implements TableCellRenderer {

    private final I18nHandler i18nHandler;
    private final TableCellRenderer delegate;

    public I18nTableCellRenderer(I18nHandler i18nHandler, TableCellRenderer delegate) {
        this.i18nHandler = i18nHandler;
        this.delegate = delegate;
    }

    @Override
    public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column
    ) {
        JLabel label = (JLabel) delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        // 将 label 的 text 字段替换为国际化后的文本。
        label.setText(i18nHandler.getMessage(label.getText()));
        return label;
    }
}
